package com.liukai.novel_info.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.Glide;
import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_info.R;
import com.liukai.novel_info.R2;
import com.liukai.novel_info.call_back.INovelRackCallBack;
import com.liukai.novel_info.presenter.impl.NovelRackPresenterImpl;

import java.util.Date;

import butterknife.BindView;

public class NovelDescActivity extends BaseActivity implements INovelRackCallBack {

    @BindView(R2.id.btn_back)
    TextView btnBack;

    @BindView(R2.id.btn_menu)
    TextView btnMenu;

    @BindView(R2.id.novel_name)
    TextView novelName;

    @BindView(R2.id.author_name)
    TextView authorName;

    @BindView(R2.id.novel_img)
    ImageView novelImg;

    @BindView(R2.id.desc_content)
    TextView descContent;

    @BindView(R2.id.add_rack)
    AppCompatButton btnAddRack;

    @BindView(R2.id.read_now)
    AppCompatButton btnReadNow;
    private NovelInfo mNovelInfo;
    private NovelRackPresenterImpl mNovelRackPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_desc;
    }

    @Override
    protected void initData(Context context) {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }

        mNovelRackPresenter = new NovelRackPresenterImpl();
        mNovelRackPresenter.registerCallback(this);

        mNovelInfo = (NovelInfo) intent.getSerializableExtra("novelInfo");


    }

    @Override
    protected void initView() {

        if (mNovelInfo == null) return;
        novelName.setText(mNovelInfo.getNovelName());
        authorName.setText(mNovelInfo.getNovelAuthor());
        descContent.setText(mNovelInfo.getNovelDesc());
        Glide.with(this)
                .load(Constans.IMAGE_BASE_URL + mNovelInfo.getNovelImg())
                .into(novelImg);

    }

    @Override
    protected void initListener() {

        // 返回按钮
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 菜单按钮
        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        // 加入书架
        btnAddRack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NovelRack novelRack = new NovelRack();
                novelRack.setNovelId(mNovelInfo.getNovelId())
                        .setUserId(Constans.getUserInfo().getUserId())
                        .setRecentlyChapter(0)
                        .setIsUpdate(0);
                LogUtils.e(getClass(), novelRack.toString());
                mNovelRackPresenter.addRack(novelRack);
            }
        });

        // 立即阅读
        btnReadNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    @Override
    public void updateRackByIdResult(Result result) {

    }

    @Override
    public void findChapterNameByIdResult(Result result) {

    }

    @Override
    public void addRackResult(Result result) {
        if (result.getCode() == 400) {
            Toast.makeText(this, result.getMsg().toString(), Toast.LENGTH_SHORT).show();
            return;
        }
        if (result.getCode() == 200) {
            Toast.makeText(this, result.getMsg().toString(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void loadMuLuResult(Result result) {

    }

    @Override
    public void findNovelChapterByIdResult(Result result) {

    }

}
